async function importPrivateKey(pem) {
  // 保持原有实现不变
  const pemHeader = "-----BEGIN PRIVATE KEY-----";
  const pemFooter = "-----END PRIVATE KEY-----";
  const pemContents = pem.replace(pemHeader, '').replace(pemFooter, '').replace(/\s/g, '');
  const binaryDer = Uint8Array.from(atob(pemContents), c => c.charCodeAt(0));
  
  return crypto.subtle.importKey(
    "pkcs8",
    binaryDer,
    { name: "RSA-OAEP", hash: "SHA-256" },
    true,
    ["decrypt"]
  );
}

self.onmessage = async (e) => {
  const { cryptoKey, chunks } = e.data;
  
  try {
    const randId=Math.round(Math.random()*16)
    console.time('worker key'+randId)
    const privateKey = cryptoKey;
    console.timeEnd('worker key'+randId)
    console.time('worker process'+randId)
    console.log('chunks size:',chunks.length);
    const decryptedChunks = await Promise.all(
      chunks.map(chunkStr => {
        // const encryptedData = Uint8Array.from(atob(chunkStr), c => c.charCodeAt(0));
        return crypto.subtle.decrypt({ name: "RSA-OAEP" }, privateKey, chunkStr);
      })
    );
    console.timeEnd('worker process'+randId)

    console.time('worker merge'+randId)
    // 合并本Worker负责的分片
    const merged = new Uint8Array(
      decryptedChunks.reduce((acc, buf) => acc + buf.byteLength, 0)
    );
    let offset = 0;
    decryptedChunks.forEach(buf => {
      merged.set(new Uint8Array(buf), offset);
      offset += buf.byteLength;
    });
    console.timeEnd('worker merge'+randId)

    // 使用Transferable提升性能
    self.postMessage({ result: merged.buffer }, [merged.buffer]);
    
  } catch (err) {
    self.postMessage({ error: err.message });
  }
};